{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test OpenAI SDK with Byzer-LLM Embeddings API\n",
    "\n",
    "This notebook demonstrates how to use the OpenAI SDK to call Byzer-LLM's embeddings API endpoint."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "import os\n",
    "\n",
    "# Configure OpenAI client to use our local server\n",
    "client = openai.OpenAI(\n",
    "    api_key=\"dummy\",  # The API key can be any string when using local server\n",
    "    base_url=\"http://localhost:8000/v1\"  # Point to local Byzer-LLM serve endpoint\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of tokens: 2\n",
      "Embedding dimension: 1024\n",
      "First few values: [-0.03998175263404846, 0.032103754580020905, -0.028294209390878677, 0.024669591337442398, -0.03315785527229309]\n"
     ]
    }
   ],
   "source": [
    "# Test single text embedding\n",
    "response = client.embeddings.create(\n",
    "    model=\"emb\",\n",
    "    input=\"Hello world\"\n",
    ")\n",
    "\n",
    "print(f\"Number of tokens: {response.usage.total_tokens}\")\n",
    "print(f\"Embedding dimension: {len(response.data[0].embedding)}\")\n",
    "print(f\"First few values: {response.data[0].embedding[:5]}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test batch text embeddings\n",
    "texts = [\n",
    "    \"The quick brown fox\",\n",
    "    \"jumps over the lazy dog\"\n",
    "]\n",
    "\n",
    "response = client.embeddings.create(\n",
    "    model=\"text-embedding-3-small\",\n",
    "    input=texts\n",
    ")\n",
    "\n",
    "print(f\"Number of embeddings: {len(response.data)}\")\n",
    "print(f\"Tokens used: {response.usage.total_tokens}\")\n",
    "for i, embedding in enumerate(response.data):\n",
    "    print(f\"\\nEmbedding {i} first few values: {embedding.embedding[:5]}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
